package com.example.a26436.newpractise.project;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.a26436.newpractise.R;
import com.example.a26436.newpractise.practise2.ArticleEnity;
import com.example.a26436.newpractise.practise2.HanderUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

/**
 * Created by 26436 on 2020/9/27.
 */

public class ProjectFragment extends Fragment {

    private RecyclerView rvArticle;
    private Context mContext;
    private Articleadapter mArticleAdapter;
    private List<ArticleEnity.DataBean.DatasBean> mArticleList = new ArrayList<>();
    private int mCurrentPage = 0;
    private SwipeRefreshLayout srlArticle;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.project_fragment_firstpage, null, false);
        rvArticle = inflate.findViewById(R.id.rv_article_project_fragment_firstpage);
        srlArticle = inflate.findViewById(R.id.srl_article_project_fragment_firstpage);
        return inflate;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initdata();
        initevent();
    }

    private void initdata() {
        mContext = getContext();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rvArticle.setLayoutManager(linearLayoutManager);
        mArticleAdapter = new Articleadapter(mContext, mArticleList);
        rvArticle.setAdapter(mArticleAdapter);
        requestArticle();

    }




    private void initevent() {
        srlArticle.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 0;
                requestArticle();

            }
        });
        rvArticle.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager layoutManager = (LinearLayoutManager) rvArticle.getLayoutManager();
                int itemCount = layoutManager.getItemCount();
                if (itemCount == 0) {
                    return;
                }
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition == itemCount - 1&&
                        newState == RecyclerView.SCROLL_STATE_IDLE) {
                    mCurrentPage +=1;
                    srlArticle.setRefreshing(true);
                    requestArticle();

                }
            }
        });


    }
    private void requestArticle() {
/*        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        builder.url(ApiInterface.ARTICLE_URL);
        Request request = builder.build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {*/
String url = String.format(ApiInterface.ARTICLE_URL,mCurrentPage);
        Log.d(TAG, "requestArticle: "+ url);
        OkhttpUtil.enqueue(url, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                ArticleEnity articleEnity = new Gson().fromJson(result, ArticleEnity.class);
                if(articleEnity.getErrorCode()== 0){
                    final List<ArticleEnity.DataBean.DatasBean> datas = articleEnity.getData().getDatas();
                    HanderUtil.post(new Runnable() {
                        @Override
                        public void run() {
                            if(mCurrentPage ==0){
                                mArticleList.clear();
                            }
                            mArticleList.addAll(datas);
                            mArticleAdapter.notifyDataSetChanged();
                            srlArticle.setRefreshing(false);


                        }
                    });

                }

            }
        });

    }
    public void scrollToTop(){
        rvArticle.scrollToPosition(0);
    }

}
